package com.example.my.agiftthat.MvpActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.my.agiftthat.R;
import com.example.my.agiftthat.config.Url;
import com.example.my.agiftthat.helper.OkHttpClientHelper;
import com.example.my.agiftthat.ui.classify.fragment.StrategyFragment;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by My on 2016/12/21.
 */

public class ClickActivity extends AppCompatActivity {
    private WebView webView_click;
    private Intent intent = null;
    private ImageView imageView_back;
    private TextView textView_search;
    private int id;
    private String url = null;
    private List<ClickBean.DataBean> mList = new ArrayList();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_click);
        intent();
    }

    private void intent() {
        webView_click = (WebView) findViewById(R.id.webView_click);
        imageView_back = (ImageView) findViewById(R.id.imageView_back);
        textView_search = (TextView) findViewById(R.id.textView_search);
        intent = getIntent();
        if (intent.getStringExtra("url") != null) {
            url = intent.getStringExtra("url");
            id = intent.getIntExtra("id", 0);
            webView_click.loadUrl(url);
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        int target_id = intent.getIntExtra("target_id", 0);
                        String jsonString = OkHttpClientHelper.getStringFromURL(ClickActivity.this, Url.HOME_VPCLICK + target_id, 1);
                        Gson gson = new Gson();
                        ClickBean bean = gson.fromJson(jsonString, ClickBean.class);
                        String url = bean.getData().getUrl();
                        Log.i("tag", "$$$$$$$$$$$$" + url);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            webView_click.loadUrl(url);
        }

        //开启JavaScript支持
        webView_click.getSettings().setJavaScriptEnabled(true);
        webView_click.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    public void clickView(View view) {
        switch (view.getId()) {
            case R.id.imageView_back:
                onBackPressed();
                break;
            case R.id.textView_search:
                Log.i("tag", "++++++++++++++++++" + id);
                intent = new Intent(ClickActivity.this, ClickSearchActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                break;
        }
    }
}
